from PyQt5.QtCore import QObject, QThread, pyqtSignal
from driver import DriverStatus
from time import time
    
class Simulation(QObject):
    update_window = pyqtSignal()
    finished = pyqtSignal()
    
    def __init__(self, scene_graph, driver, timestep, parent = None):
        super(Simulation,self).__init__(parent)
        self.scene_graph = scene_graph
        self.driver = driver
        self.timestep = timestep
        
    def start(self):
        self.scene_graph.initSimulationResourceGraph()
        print('initialize simulation resource successfully')
        while self.driver.status != DriverStatus.FINISHED:
            if self.driver.status == DriverStatus.STOPPED:
                QThread.yieldCurrentThread()
            elif self.driver.status == DriverStatus.RUNNABLE:
                time_start = time()
                self.scene_graph.runGraph(self.timestep)
                self.scene_graph.transformDrawDataGraph()
                time_end = time()
                print('time per frame:%fs' % (time_end-time_start))
                self.update_window.emit()
                
        self.finished.emit()